package com.network.vo;

import com.network.entity.Role;
import com.network.qo.UserQo;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author peng
 */
public class SecurityUser extends UserQo implements UserDetails {

    public SecurityUser() {
    }

    public SecurityUser(UserQo userQo)
    {
        this.setId(userQo.getId());
        this.setUsername(userQo.getUsername());
        this.setPassword(userQo.getPassword());
        this.setEmail(userQo.getEmail());
        this.setMobile(userQo.getMobile());
        this.setStatus(userQo.getStatus());
        this.setDept(userQo.getDept());
        this.setRoles(userQo.getRoles());
        this.setDelFlag(userQo.getDelFlag());
        this.setCreateTime(userQo.getCreateTime());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities()
    {

        Collection<GrantedAuthority> authorities=new ArrayList<>();
        List<Role> roles= this.getRoles();

        if (roles!=null)
        {
            for (Role role:roles)
            {
                SimpleGrantedAuthority authority=new SimpleGrantedAuthority(role.getName());
                authorities.add(authority);
            }
        }
        return authorities;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
